<?php
    /**
    * signup.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    require_once 'Validate.php';
    
    /**
    * The signup application accepts applications for user signups.
    * @package Atman
    */
    class signup extends AT_Module
    {
        /**
        * Constructor.
        * @param mixed $options Unused.
        */
        public function __construct()
        {
            parent::__construct('users');
        }
        
        /**
        * The default event simply displays a signup form and any errors from
        * previous signup attempts.
        * @return void
        */
        public function __default()
        {
            $this->setView(AT_View::factory('smarty', $this));
            
            $session = AT_Session::singleton();
            
            if ( $session->get('errors') != NULL )
            {
                $this->set('errors', $session->get('errors'));
                $session->set('errors', NULL);
            }
            
            $this->set('pageTitle', 'Signup');
        }
        
        /**
        * Checks the form data for user registration.
        * @return void
        */
        public function send()
        {
            if ( sizeof($_POST) > 0 )
            {
                $email = $_POST['signup_email'];
                $password = $_POST['signup_password'];
                $confirm = $_POST['signup_confirm'];
                $firstName = $_POST['signup_fname'];
                $lastName = $_POST['signup_lname'];
                
                $nameMatch = '/[A-Za-z\'-]{2,32}/';
                
                $errors = array();
                
                if ( !Validate::email($email) )
                {
                    $errors[] = 'Invalid email address.';
                }
                
                if ( $password != $confirm )
                {
                    $errors[] = 'Passwords do not match.';
                }
                else if ( strlen($password) < 6 )
                {
                    $errors[] = 'Password must be atleast 6 characters long.';
                }
                
                if ( !preg_match($nameMatch, $firstName) )
                {
                    $errors[] = 'Invalid first name.';
                }
                
                if ( !preg_match($nameMatch, $lastName) )
                {
                    $errors[] = 'Invalid last name.';
                }
                
                if ( sizeof($errors) == 0 )
                {
                    $users = new AT_Users;
                    $data = array
                    (
                        'email' => $email,
                        'password' => AT_User::hashPassword($password),
                        'firstName' => $firstName,
                        'lastName' => $lastName,
                        'joinDate' => date('Y-m-d H:i:s', time()),
                        'status' => 'active'
                    );
                    $users->insert($data);
                    
                    header('Location: '.AT_SITE_ROOT.'/student');
                }
                else
                {
                    $session = AT_Session::singleton();
                    $session->set('errors', $errors);
                    
                    header('Location: '.AT_SITE_ROOT.'/users/signup');
                    exit();
                }
            }
            else
            {
                header('Location: '.AT_SITE_ROOT.'/users/signup');
                exit();
            }
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
?>
